using System.Collections;
using System.Collections.Generic;
using ProjectSurvival.System.Save;
using UnityEngine;
using QFramework;
using NotImplementedException = System.NotImplementedException;

namespace ProjectSurvival
{
    public class CoinUpgradeSystem : AbstractSystem,ICanSave
    {
        public List<CoinUpgradeItem> CoinUpgradeItems { get; } = new();
        
        public static EasyEvent OnUpgradeStateChanged = new();

        public CoinUpgradeItem AddItem(CoinUpgradeItem item)
        {
            CoinUpgradeItems.Add(item);
            return item;
        }
        protected override void OnInit()
        {
            AddItem(new CoinUpgradeItem()
                .WithKey("coin_percent_LV1")
                .WithDescription($"金币掉落概率Lv1")
                .WithPrice(100)
                .OnUpgrade((item) =>
                {
                    Global.CoinPercent.Value += 0.1f;
                    Global.Coin.Value -= item.Price;
                }))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("coin_percent_LV2")
                    .WithDescription($"金币掉落概率Lv2")
                    .WithPrice(1000)
                    .OnUpgrade((item) =>
                    {
                        Global.CoinPercent.Value *= 0.1f;
                        Global.Coin.Value -= item.Price;
                    })))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("coin_percent_LV3")
                    .WithDescription($"金币掉落概率Lv3")
                    .WithPrice(2000)
                    .OnUpgrade((item) =>
                    {
                        Global.CoinPercent.Value += 1.1f;
                        Global.Coin.Value -= item.Price;
                    })))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("coin_percent_LV4")
                    .WithDescription($"金币掉落概率Lv4")
                    .WithPrice(5000)
                    .OnUpgrade((item) =>
                    {
                        Global.CoinPercent.Value += 1.1f;
                        Global.Coin.Value -= item.Price;
                    })))
                ;

            
            var expPercentItem = AddItem(new CoinUpgradeItem()
                .WithKey("exp_percent")
                .WithDescription($"经验掉落概率")
                .WithPrice(5)
                .OnUpgrade((item) =>
                {
                    Global.ExpPercent.Value *= 1.1f;
                    Global.Coin.Value -= item.Price;
                }));
            AddItem(new CoinUpgradeItem()
                .WithKey("max_Hp1")
                .WithDescription($"玩家最大生命值Lv1")
                .WithPrice(1000)
                .OnUpgrade((item) =>
                {
                    Global.MaxHp.Value += 1;
                    Global.Coin.Value -= item.Price;
                }))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("max_Hp2")
                    .WithDescription($"玩家最大生命值Lv2")
                    .WithPrice(2000)
                    .OnUpgrade((item) =>
                    {
                        Global.MaxHp.Value += 1;
                        Global.Coin.Value -= item.Price;
                    })))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("max_Hp3")
                    .WithDescription($"玩家最大生命值Lv3")
                    .WithPrice(3000)
                    .OnUpgrade((item) =>
                    {
                        Global.MaxHp.Value += 1;
                        Global.Coin.Value -= item.Price;
                    })))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("max_Hp4")
                    .WithDescription($"玩家最大生命值Lv4")
                    .WithPrice(4000)
                    .OnUpgrade((item) =>
                    {
                        Global.MaxHp.Value += 1;
                        Global.Coin.Value -= item.Price;
                    })))
                .Next(AddItem(new CoinUpgradeItem()
                    .WithKey("max_Hp5")
                    .WithDescription($"玩家最大生命值Lv5")
                    .WithPrice(5000)
                    .OnUpgrade((item) =>
                    {
                        Global.MaxHp.Value += 1;
                        Global.Coin.Value -= item.Price;
                    })))
                ;
            //加载配置
            Load();
            
            //金币升级系统状态变更时保存
            OnUpgradeStateChanged.Register(() => Save());
        }

        public void Save()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in CoinUpgradeItems)
            {
                saveSystem.SaveBool(coinUpgradeItem.Key,coinUpgradeItem.UpgradeFinish);
            }
        }

        public void Load()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in CoinUpgradeItems)
            {
                coinUpgradeItem.UpgradeFinish = saveSystem.LoadBool(coinUpgradeItem.Key, false);
            }
        }
    }
}
